/**
 * ParserManager.java
 * Determines DataParser which can parse a select File extension.
 * @authors: Connor Gordon, Difan Zhao, Kathleen Lan, Patrick Royal
 */

package io;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.ResourceBundle;


/**
 * @author Group 8
 */
public final class ParserManager {

    private static ResourceBundle ourDataParsers = ResourceBundle.getBundle("resources.Parser");

    private ParserManager () {
    }

    /**
     * Returns DataParser that can read the selected file extension
     * 
     * @param ext file extension
     * @param name element classification
     * @param id unique string identifying the parser
     * @param url location of data
     * @return DataParser that can read selected file type
     */
    public static DataParser selectParser (String ext, String name, String id, String url) {
        try {
            Constructor<?> c = Class.forName(
                    ourDataParsers.getString(ext)).getConstructor(
                            String.class, String.class, String.class);
            return (DataParser) c.newInstance(name, id, url);
        }
        catch (ClassNotFoundException e) {
            System.out.println(
                    "Unable to find DataParser associated with file type.");
        }
        catch (NoSuchMethodException e) {
            e.printStackTrace();
        }
        catch (SecurityException e) {
            e.printStackTrace();
        }
        catch (InstantiationException e) {
            System.out.println(
                    "Cannot instantiate DataParser with the data given");
        }
        catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        catch (IllegalArgumentException e) {
            e.printStackTrace();
        }
        catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        return null;
    }
}
